<template>
  <div>
    <div class="jifen-table">
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <!--prop 循环项-->
        <el-table-column
            prop="fxScore"
            align="center"
            label="分享积分"
            width="240">
        </el-table-column>
        <el-table-column
            prop="jlScore"
            label="奖励积分"
            align="center"
            width="240">
        </el-table-column>
        <el-table-column
            prop="zcScore"
            label="注册积分"
            align="center"
            width="240">
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="success"
                @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--点击弹出框-->
    <el-dialog title="编辑信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">

        <el-form-item label="分享积分" :label-width="formLabelWidth">
          <el-input v-model="form.fxScore" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="奖励积分" :label-width="formLabelWidth">
          <el-input v-model="form.jlScore" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册" :label-width="formLabelWidth">
          <el-input v-model="form.zcScore" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upok">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "jifen",
    data() {
      return {
        // token: '$2a$10$lZrcfKnKCrDTsCvs1jEEi.Y9wpCnpqKWow2Pj6dCVnwhZXXuj/9Pi-15b4a5f1-b8ed-4fdb-a69b-534dbc58f07d',
        tableData: [],
        dialogFormVisible: false,
        form: {
          fxScore: '',
          jlScore: '',
          zcScore: '',
        },
        formLabelWidth: '100px'
      }
    },
    methods: {
      getlist() {//获取数据
        this.axios.post(this.API.API1 + '/getScore', {}, {
          headers: {
            'Authorization': 'XIEGE ' + this.API.token,
            'Content-Type': 'application/json;charset=UTF-8'
          }
        }).then(res => {
          console.log(res)
          this.tableData.push(res.data)
        })
      },

      upok() {//确认修改
        console.log(this.form)
        this.$confirm('是否修改信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(this.API.API1 + '/updScore', this.form, {
            headers: {
              'Authorization': 'XIEGE ' + this.API.token,
              'Content-Type': 'application/json;charset=UTF-8'
            }
          }).then(res => {
            if (res.data === "success") {
              //置空
              this.tableData = []
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
            }
            this.dialogFormVisible = false
            this.getlist()

            console.log(res)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      handleEdit(i, r) {//编辑方法
        console.log(i)
        console.log(r)
        //回填表单
        this.form = r
        this.dialogFormVisible = true
      },

      handleDelete(i, r) {//删除方法
        // console.log(i)
        // console.log(r)
        this.tableData.forEach((item, index) => {
          if (i === index) {
            this.tableData.splice(index, 1)
          }
        })
      },

    },
    created() {
      this.getlist()
    },
  }
</script>

<style scoped>
  .jifen-table {
    width: 100%;
  }

</style>